package ${packages.blockchain};

import ${packages.dto}.${classInfo.className}CreateRequest;
import ${packages.dto}.${classInfo.className}Response;
import ${packages.fiscoConfig}.BlockchainConfig;
import ${packages.fiscoService}.BlockchainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ${classInfo.className} 链上存证 Facade，示例逻辑需按业务自行调整。
 */
@Service
@RequiredArgsConstructor
@Slf4j
@ConditionalOnProperty(prefix = "feature.blockchain", name = "enabled", havingValue = "true", matchIfMissing = true)
public class ${classInfo.className}BlockchainFacade {

    private final BlockchainService blockchainService;
    private final BlockchainConfig blockchainConfig;

    public String saveToChain(${classInfo.className}CreateRequest request) {
        try {
            // TODO: 根据业务字段构造合约调用参数
            return blockchainService.callContract("save${classInfo.className}", List.of());
        } catch (Exception e) {
            log.error("链上存证失败", e);
            return null;
        }
    }

    public ${classInfo.className}Response queryFromChain(Long id) {
        try {
            String result = blockchainService.queryContract("query${classInfo.className}", List.of(String.valueOf(id)));
            ${classInfo.className}Response response = new ${classInfo.className}Response();
            // TODO: 根据合约返回结果填充 response 字段
            response.setId(id);
            return response;
        } catch (Exception e) {
            log.error("链上查询失败", e);
            return null;
        }
    }

    public String getContractName() {
        return blockchainConfig.getContractName();
    }
}
